package cn.itcast.controller;

import cn.itcast.constant.MessageConstant;
import cn.itcast.entity.PageResult;
import cn.itcast.entity.QueryPageBean;
import cn.itcast.entity.Result;
import cn.itcast.pojo.Permission;
import cn.itcast.service.PermissionService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;

@RestController
@RequestMapping("/permission")
@CrossOrigin
public class PermissionController {

    @Reference
    private PermissionService permissionService;

    /**
     * 据菜单id查询对应的权限
     * @param menuIds
     * @return
     */
    //根据菜单id查询对应的权限-- from罗年羽
    @GetMapping("/menu/association")
    public Result menuByIds(@RequestParam Integer[] menuIds){
        try {
            ArrayList<Permission>arr =permissionService.selectMenuIdorPermission(menuIds);
            return new Result(true,MessageConstant.QUERY_PERMISSION_SUCCESS,arr);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_PERMISSION_FAIL);
        }
    }


    /**
     * 根据条件 查询权限信息
     * @param queryPageBean
     * @return
     */
    @GetMapping
    public Result findAllPermission(QueryPageBean queryPageBean){

        try {
            Integer currentPage = queryPageBean.getCurrentPage();
            Integer pageSize = queryPageBean.getPageSize();
            String queryString = queryPageBean.getQueryString();

            PageResult pr= permissionService.findAllPermission(currentPage,pageSize,queryString);

            return new Result(true,MessageConstant.QUERY_PERMISSION_SUCCESS,pr);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_PERMISSION_FAIL);
        }
    }


    /**
     * 新增权限
     * @param permission
     * @return
     */
    @PostMapping
    public Result addPermission(@RequestBody Permission permission){

        try {
            permissionService.addPermission(permission);
            return new Result(true,MessageConstant.ADD_PERMISSION_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_PERMISSION_FAIL);
        }


    }


    /**
     * 根据权限id 查询权限
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result findPermissionByPermissionId(@PathVariable Integer id){

        try {
            Permission permission= permissionService.findPermissionByPermissionId(id);
            return new Result(true,MessageConstant.QUERY_PERMISSION_SUCCESS,permission);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_PERMISSION_FAIL);
        }


    }


    /**
     * 更新权限信息
     * @param permission
     * @return
     */
    @PutMapping
    public Result updatePermission(@RequestBody Permission permission){

        try {
            permissionService.updatePermission(permission);
            return new Result(true,MessageConstant.EDIT_PERMISSION_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_PERMISSION_FAIL);
        }
    }


    /**
     *
     * 根据id 删除权限
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public Result deletePermissionById(@PathVariable Integer id){
        try {
            String message= permissionService.deletePermissionById(id);
            return new Result(true,message);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_PERMISSION_FAIL);
        }
    }


}
